package com.ironman.h6b1gw.jxls;

import com.ironman.h6b1gw.jxls.model.Employee;
import org.jxls.template.SimpleExporter;

import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class Jxls2Demo {

    public static void main(String[] args) throws Exception {
        Path templatePath = Paths.get("C:/Users/89754/Desktop/jxls/template2.xlsx");
        Path outPath = Paths.get("C:/Users/89754/Desktop/jxls/out2.xlsx");
        if (Files.exists(outPath)) {
            Files.delete(outPath);
        }
        if (Files.exists(templatePath)) {
            List<String> headers = Arrays.asList("姓名", "年龄");
            SimpleExporter exporter = new SimpleExporter();
            try (InputStream templateStream = Files.newInputStream(templatePath);
                 OutputStream outputStream = Files.newOutputStream(outPath)) {
                exporter.registerGridTemplate(templateStream);
                exporter.gridExport(headers, Employee.employees(), "name, age", outputStream);
            }
        } else {
            System.err.println("模板文件不存在");
        }
    }
}